package huhui.com.recyclerviewtest;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
  private List<Fruit> mFruitList;
  static class ViewHolder extends RecyclerView.ViewHolder{
      ImageView fruitImage;
      TextView fruitName;

      public ViewHolder(View view)
      {
          super(view);
          fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
          fruitName=(TextView)view.findViewById(R.id.fruit_name);

      }
  }

  public FruitAdapter(List<Fruit> fruitList)
  {
      mFruitList=fruitList;
  }

  public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
  {
      View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
      ViewHolder holder=new ViewHolder(view);
      return holder;
  }

  public void onBindViewHolder(ViewHolder holder,int position)
  {
      Fruit fruit=mFruitList.get(position);
      holder.fruitImage.setImageResource(fruit.getImageId());
      holder.fruitName.setText(fruit.getName());
  }

  public int getItemCount()
  {
      return mFruitList.size();
  }

}
